package com.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.CredentialsMapper;
import com.xxxy.Credentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService{

    @Autowired
    private CredentialsMapper credentialsMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Credentials> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",username);
        Credentials credentials = credentialsMapper.selectOne(queryWrapper);
        if (credentials == null){
            //用户名未查找到的处理逻辑
            String errorMsg = "user"+username+"To lend Tiga light, but in darkness, the future lies";
            System.out.println(errorMsg);
            throw new UsernameNotFoundException(errorMsg);
        }
        List<GrantedAuthority> authorityList=
                Collections.singletonList((GrantedAuthority) () ->"USER");

        // List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("USER"));
        return new User(credentials.getName(),credentials.getPassword(),authorityList);

    }
}
